﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes.Banco
{
    public class clsBanco
    {
        private MySqlCommand _sql;

        public MySqlCommand Sql
        {
            get { return _sql; }
            set { _sql = value; }
        }
        private string _strConexao;
        private MySqlConnection _conn;
        private MySqlTransaction _transacao;

        public clsBanco()
        {
            _strConexao = @"Server=localhost;user id=root;password=1234;database=rfidnew";
            _conn = new MySqlConnection(_strConexao);
            _sql = new MySqlCommand();
            _sql.Connection = _conn;
        }

        public bool AbreConexao()
        {
            try
            {
                if (_conn.State == ConnectionState.Closed)
                    _conn.Open();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool FechaConexao()
        {
            try
            {
                if (_conn.State == ConnectionState.Open)
                    _conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public int ExecultaComando()
        {
            try
            {
                AbreConexao();
                return _sql.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                return 0;
            }
            finally
            {
                FechaConexao();
                //Sql = null;
            }
        }

        public DataTable RetornaDados()
        {
            try
            {
                DataTable dt = new DataTable();
                AbreConexao();

                dt.Load(_sql.ExecuteReader());
                return dt;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                FechaConexao();
                //Sql = null;
            }
        }
    }
}
